import api from '../../api/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
		active: 'all',
    personOrderlist: [],
    companylist:[],
		page: 1,
		row: 10,
    total: 0,
    phone:'',
    type:'',
    searchCondition:'',
    materialSearchCondition:'',
  },
  onChange(e) {
		let that = this
		that.setData({
			active: e.detail.name,
			page: 1,
			// listItems: []
    })
    that.getPhone()
	
	},
  goToMyBack(){
    wx.navigateBack()
  },
  goToUserEvaluation(data){
    let shippingOrderNumber = data.currentTarget.dataset.index
    wx.navigateTo({
      url:'/pages/userEvaluation/userEvaluation?shippingOrderNumber='+shippingOrderNumber
    })
  },

  onSearch(e){
  let that = this
  that.data.searchCondition =e.detail
  that.getpersonalorder()
  // console.log(that.data.searchCondition,'11111');
  },
  onClick(){
    let that = this;
    that.getpersonalorder()
  },
  clearSearch(){
    let that = this;
    that.getpersonalorder()
  },
  getmateriallist() {
    try {
     let that = this;
    //  let value = wx.getStorageSync('token')
     let userid=wx.getStorageSync('id')
     api.post('/app/logistics/getOrderInfo', {companyId:that.data.companyid,id:userid,shippingOrderNumber:""})
     .then(res => {
     })
    } catch (error) {
      console.log("error")
    }
 },
  getPhone(){
    let that =this
    let id = wx.getStorageSync('id')
    api.post('/userInfoApp/getById', {id:id}).then(res => {
      that.setData({
        phone:res.data.phone
      })
      that.getpersonalorder(res.data.phone)
   })
 
  },
  getpersonalorder(e) {
    // 改变this指向  
    let that = this;
    let id = wx.getStorageSync('id') 
    that.setData({
      id:id,
    })
		let userInfoReq = {
				shippingStatus: that.data.active,
				page: that.data.page,
        row: that.data.row,
        searchCondition:that.data.searchCondition,
        id:that.data.id,
        phone:that.data.phone
      }
      // console.log('请求参数', userInfoReq)
      if(userInfoReq.shippingStatus == 'all'){
        userInfoReq.shippingStatus = ''
      }
    api.post('/userInfoApp/findUserPage', userInfoReq).then(res => {
       that.setData({
        personOrderlist:res.data,
        total:res.data.total
       })
    })
    .catch(err => {
    wx.showToast({
      icon:'none',
      title: err.message,
      duration: 2000
    })
   })
  },
  goToDetail(data){
    let shippingOrderNumber = data.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/orderDetail/orderDetail?shippingOrderNumber=`+shippingOrderNumber,
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
      }
    })
  },

  //物流用户接口
  materialonSearch(e){
    let that = this
    that.setData({
      materialSearchCondition:e.detail
    })
    that.getCompanyOrder()
    },
    materialonClick(){
      let that = this;
      that.getCompanyOrder()
    },
    materialonChange(e) {
      let that = this
      that.setData({
        active: e.detail.name,
        page: 1,
        // listItems: []
      })
     that.getCompanyOrder()
    },
    getCompanyOrder(){
      let that = this;
      let companyid = wx.getStorageSync('companyid')
      that.setData({
        companyid:companyid,
      })
      let companyInfoReq = {
        shippingStatus: that.data.active,
        page: that.data.page,
        row: that.data.row,
        searchCondition:that.data.materialSearchCondition,
        companyId:that.data.companyid
      }
      if(companyInfoReq.shippingStatus == 'all'){
        companyInfoReq.shippingStatus = ''
      }
      api.post('/companyInfoApp/findCompanyPage',companyInfoReq).then(res => {
          that.setData({
            companylist :res.data,
            total: res.data.total
          })
      })
    },
    goToUpdataLogistics(data){
      let shippingOrderNumber = data.currentTarget.dataset.index
      wx.navigateTo({
        url:'/pages/updateLogistics/updateLogistics?shippingOrderNumber='+shippingOrderNumber
      })
    },
    goToReceipt(data){
      let shippingOrderNumber = data.currentTarget.dataset.index
      wx.navigateTo({
        url:'/pages/receipt/receipt?shippingOrderNumber='+shippingOrderNumber
      })
    },
    arrivalNotice(data){
      let that =this
      let shippingOrderNumber = data.currentTarget.dataset.index
      var id = wx.getStorageSync('id')
      var companyid = wx.getStorageSync('companyid')
      let params={
       id:id,
       companyId:companyid,
       shippingOrderNumber:shippingOrderNumber
      }
      api.post('/app/logistics/arrivalNotice',params).then(res => {
        that.getCompanyOrder()
      })
    },
   goToOrderDetail(data){
    let shippingOrderNumber = data.currentTarget.dataset.index
      wx.navigateTo({
        url:'/pages/orderDetail/orderDetail?shippingOrderNumber='+shippingOrderNumber
      })
    },
    immediateDelivery(data){
      let that =this 
     let shippingOrderNumber = data.currentTarget.dataset.index
     var id = wx.getStorageSync('id')
     var companyid = wx.getStorageSync('companyid')
     let params={
      userId:id,
      companyId:companyid,
      shippingOrderNumber:shippingOrderNumber
     }
      api.post('/companyInfoApp/updateStatus',params).then(res => {
          that.getCompanyOrder()
    })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {//此处接收传递过来的参数wx.navigateTo跳转时传递的参数 
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this
    var type = wx.getStorageSync('type')
    let value = wx.getStorageSync('companyid')
    //如果要在页面中使用
    that.setData({
      type:type,
      companyid:value
    })
    that.getmateriallist() 
    if(type == 2){
      that.getPhone()
    }else{
      that.getCompanyOrder()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
		let that = this
    if (that.data.page * 10 < that.data.total) {
      that.setData({
        page: that.data.page += 1
      })
      that.getpersonalorder()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})